package com.neuedu.controller.workrule;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.neuedu.common.DBUtil;
import com.neuedu.factory.ClassFactory;
import com.neuedu.service.UserService;
import com.neuedu.service.WorkRuleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "AddWorkRuleServlet", urlPatterns = "/his/addworkrule")
public class AddWorkRuleServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        String startdate2 = request.getParameter("startdate2");
        String enddate2 = request.getParameter("enddate2");
        String selectdeptdialog = request.getParameter("selectdeptdialog");
        String ruleName = request.getParameter("ruleName");
        Object[] objs = {ruleName, selectdeptdialog, startdate2, enddate2};
        Connection con = DBUtil.getCon();
        WorkRuleService wrs = (WorkRuleService) ClassFactory.createObj("WorkRuleService");
        int wrokruleid = 0;
        try {
            con.setAutoCommit(false);
            wrokruleid = wrs.addWorkRule(objs, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            System.out.println("-1");
            throw new RuntimeException("addWrokRuleServlet");
        }
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        int allLength = Integer.parseInt(request.getParameter("allLength[0]"));
        int len = Integer.parseInt(request.getParameter("len[0]"));
        for (int i = 0; i < allLength; i++) {
            map = new HashMap<>();
            for (int j = 0; j < len; j++) {
                map.put("workinfo", request.getParameter("checkeds[" + i + "]"));
                map.put("deptid", request.getParameter("selectdeptdialog"));
                map.put("workruleid", wrokruleid);
                map.put("name", request.getParameter("name[" + i + "]"));
                map.put("userid", request.getParameter("id[" + i + "]"));
            }
            list.add(map);
        }
        int flag = 0;
        ObjectMapper om = new ObjectMapper();

        try {
            flag = wrs.addWrokDocInfo(list, con);
            String text = om.writeValueAsString(flag);
            response.getWriter().append(text);
            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            System.out.println(0);
            throw new RuntimeException("addWrokDocInfoServlet");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            System.out.println(1);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(2);
        } finally {
            DBUtil.close(con);

        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
